﻿using B.WMS.StandardReturn;
using B.WMS.WMSDtos.Snventory;
using B.WMS.WMSDtos.Warehousety;
using B.WMS.WMSEntities.Snventory;
using B.WMS.WMSEntities.Warehousety;
using B.WMS.WMSStaticEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace B.WMS.Snventory
{
    public class ReviewServices : ApplicationService, IReviewServices
    {
        private readonly IRepository<ReviewModel, Guid> _reviewRepository;
        private readonly IRepository<Warehouse, Guid> _WarehouseRepository;
        public ReviewServices(IRepository<ReviewModel, Guid> reviewRepository, IRepository<Warehouse, Guid> WarehouseRepository)
        {
            _reviewRepository = reviewRepository;
            _WarehouseRepository = WarehouseRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResult<ReviewModelDto>> AddReview(CreateUpdateReviewDto input)
        {
            var reviews = ObjectMapper.Map<CreateUpdateReviewDto, ReviewModel>(input);
            reviews.ReviewNo = AutoCode.AutoCode.GetCode("CK").Result.ResultData;
            await _reviewRepository.InsertAsync(reviews);
            return new ApiResult<ReviewModelDto>() { Code = ApiCode.Success, ResultData = ObjectMapper.Map<ReviewModel, ReviewModelDto>(reviews) };
        }

        public async Task<ApiResult<ReviewModel>> DeleteReview(string id)
        {
            var reviews = await _reviewRepository.GetAsync(x=>x.ReviewNo==id);
            await _reviewRepository.DeleteAsync(reviews);
            return new ApiResult<ReviewModel>() { Code = ApiCode.Success, ResultData = reviews };
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<ReviewModelDto>> GetReview()
        {
            var reviews = await _reviewRepository.GetListAsync();
            var warehouse = await _WarehouseRepository.GetListAsync();
            var link = from r in reviews
                       join w in warehouse on r.Warehouseid equals w.WarehouseNo
                       select new ReviewModelDto {
                         Warehouseid=r.Warehouseid,
                          EndTime=r.EndTime,
                           Remark=r.Remark,
                            Rstauts=r.Rstauts,
                             ReviewNo=r.ReviewNo,
                              ReviewType=r.ReviewType,
                               StartTime=r.StartTime,
                                WarehouseName=w.Warename
                       };
            return link.ToList();
        }

        public async Task<ApiPaging<ReviewModelDto>> GetReviewInput(PageInput input)
        {
            var query=await _reviewRepository.GetQueryableAsync();
            var totalData = query.Count();
            var pageDatas = query.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var pageDtos = ObjectMapper.Map<List<ReviewModel>, List<ReviewModelDto>>(pageDatas);
            var totalPage = (int)Math.Ceiling((double)totalData / input.PageSize);
            return new ApiPaging<ReviewModelDto>
            {
                Code = ApiCode.Success,
                TotalData = totalData,
                TotalPage = totalPage,
                PageDatas = pageDtos
            };
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResult<ReviewModelDto>> UpdateReview(CreateUpdateReviewDto input)
        {
            var reviews = await _reviewRepository.FirstOrDefaultAsync(x=>x.ReviewNo==input.ReviewNo);
            if (reviews == null)
            {
                return new ApiResult<ReviewModelDto> { Code = ApiCode.UrlError, Message = "盘点单不存在" };
            }
            ObjectMapper.Map(input, reviews);
            await _reviewRepository.UpdateAsync(reviews);
            return new ApiResult<ReviewModelDto>
            {
                Code = ApiCode.Success,
                ResultData = ObjectMapper.Map<ReviewModel, ReviewModelDto>(reviews)
            };
        }
    }
}
